﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using VOL;
using DAL;

namespace BLL
{
    public class ChungLoaiBL
    {
        private DataAccess dataAccess;
        public ChungLoaiBL()
        {
            dataAccess = new DataAccess();
        }

        public DataTable GetData()
        {
            string strSql = "SELECT * FROM tbl_ChungLoai ORDER BY MaChungLoai DESC ";
            return dataAccess.GetData(strSql);
        }

        //Thêm
        public bool insertChungLoai(ChungLoai obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@TenChungLoai", SqlDbType.NVarChar) { Value = obj.TenChungLoai };

            string strSql = "INSERT INTO tbl_ChungLoai(TenChungLoai) VALUES (@TenChungLoai)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        ////Xóa
        public bool deleteChungLoai(ChungLoai obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaChungLoai", SqlDbType.Int) { Value = obj.MaChungLoai };

            string strSql = "DELETE FROM tbl_ChungLoai WHERE MaChungLoai = @MaChungLoai";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Sửa
        public bool updateChungLoai(ChungLoai obj)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@TenChungLoai", SqlDbType.NVarChar) { Value = obj.TenChungLoai };
            param[1] = new SqlParameter("@MaChungLoai", SqlDbType.Int) { Value = obj.MaChungLoai };

            string strSql = "UPDATE tbl_ChungLoai SET " +
                            "TenChungLoai = @TenChungLoai " +
                            "WHERE MaChungLoai = @MaChungLoai";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Tìm theo Mã chủng loại
        public DataTable searchIDChungLoai(int maChungLoai)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaChungLoai", SqlDbType.Int) { Value = maChungLoai };

            string strSql = "SELECT * FROM tbl_ChungLoai WHERE MaChungLoai = @MaChungLoai";
            return dataAccess.GetData(strSql, param);
        }
    }
}
